//
//  DLAboutRecSV.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/11/30.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLAboutRecSV.h"

@implementation DLAboutRecSV

- (instancetype) initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        
        [self layoutSubControls];
        self.backgroundColor = kDLMachineTableAndCellBgColor;
//        self.backgroundColor = [UIColor whiteColor];
//        self.backgroundColor = kColorRGB(240, 240, 240);
    }
    return self;
}
- (void) layoutSubControls {
    
    
}

- (void) setDataSource:(NSArray *)dataSource {
    _dataSource = dataSource;
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
//    NSLog(@"关联推荐的数据源是：%@", dataSource);
    if (dataSource.count) {
        
        NSMutableArray *buttonWidthMutArr = [NSMutableArray array];
        CGFloat contentSizeWidth = 0.0;
        CGFloat lastButtonWidth = 0.0;
        
        for (NSInteger i = 0; i < dataSource.count; i++) {
            NSDictionary *dic = dataSource[i];
            NSString *showText = kSafeString(dic[@"showtext"]);
            
            //宽度不一样，当前按钮的x值 是左边所有按钮的宽度总和 + 间距
            CGFloat buttonWidth = [showText getValue4WidWithHeight:30 andFont:kSystemFont(14)];
            buttonWidth += 10;
            NSString *buttonWidthStr = [NSString stringWithFormat:@"%.2f", buttonWidth];
            [buttonWidthMutArr addObject:buttonWidthStr];
            
            UIButton *recButton = [UIButton buttonWithType:0];
            if (i == 0) {
                recButton.frame = CGRectMake(10, 5, buttonWidth, 30);
            } else {
                lastButtonWidth = [self getLeftButtonsWidth:buttonWidthMutArr withIndex:i];
                recButton.frame = CGRectMake(10 + lastButtonWidth + 10 * i, 5, buttonWidth, 30);
            }
            recButton.backgroundColor = kColorRGB(30, 108, 227);
            [recButton setBackgroundImage:[UIImage imageNamed:@"aboutRecButtonBgView"] forState:UIControlStateNormal];
            [recButton setTitle:showText forState:UIControlStateNormal];
            [recButton setTitleColor:kDLMachineTableAndCellBgColor forState:UIControlStateNormal];
            [recButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            recButton.titleLabel.font = kSystemFont(14);
            recButton.tag = 100 + i;
//            recButton.layer.cornerRadius = 15;
//            recButton.clipsToBounds = YES;
            [recButton addTarget:self action:@selector(recButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:recButton];
            contentSizeWidth += recButton.width;
        }
        self.contentSize = CGSizeMake(contentSizeWidth + (dataSource.count + 1) * 10, 40);
        NSLog(@"关联推荐的sv的contentsize的宽度是：%f", self.contentSize.width);
    }
}
- (CGFloat) getLeftButtonsWidth:(NSMutableArray *)mutArr withIndex:(NSInteger)index{
    CGFloat leftButtonsWidth = 0.0;
    for (NSInteger i = 0; i < index; i++) {
        NSString *buttonWidthStr = [mutArr objectAtIndex:i];
        leftButtonsWidth += buttonWidthStr.floatValue;
    }
    return leftButtonsWidth;
}
- (void) recButtonClick:(UIButton *)button {
    
    if (self.recButtonClickBlock) {
        self.recButtonClickBlock(button.tag - 100);
    }
}

@end
